package com.goldgov.statistics.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.goldgov.information.service.ArticleService;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.statistics.service.StatisticsBQSchedule;
import com.goldgov.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/portal/statistics")
@Api("统计获取外网信息接口接口")
public class StatisticsController {

	@Autowired
	private ArticleService articleService;
	
	@GetMapping("/getTime")
	@ApiOperation(value = "统计首页获取网络时间", notes ="")
	public JsonObject<Object> getTime(HttpServletRequest request,HttpServletResponse response) {
		long time = DateUtil.getWebsiteDatetime("http://www.baidu.com");
		if(time>0){
			JsonSuccessObject.SUCCESS.setMessage(time+"");
//			Date da = new Date(time);
			/*SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
			String dada = sdf.format(da);*/
			JsonSuccessObject.SUCCESS.setData("netDate");
		}else{
			Date date = new Date();
			JsonSuccessObject.SUCCESS.setMessage(date.getTime()+"");
			JsonSuccessObject.SUCCESS.setData("sysDate");
		}	
		return JsonSuccessObject.SUCCESS;
	}
	
	//文章标签热度统计
	@GetMapping("/statisticsBQ")
	public JsonObject<Object> statisticsBQ(HttpServletRequest request,HttpServletResponse response) {
		JsonSuccessObject.SUCCESS.setMessage("查询成功!");
		JsonSuccessObject.SUCCESS.setData(StatisticsBQSchedule.bqList);
		return JsonSuccessObject.SUCCESS;
	}
	
	@GetMapping("/statisticsBQq")
	@Deprecated//弃用此方法,请去cms查询
	public JsonObject<Object> statisticsBQq(HttpServletRequest request,HttpServletResponse response) {
		List<Object[]> bqListTemp=new ArrayList<Object[]>();
		Map<String,Integer> aMap=new HashMap<>();
		Map<String,Integer> uMap=new HashMap<>();
		List<String> articleBQ=articleService.statisticsBQByArticle();
		for(String s:articleBQ){
			for(String ss:s.split(",")){
				aMap.put(ss.trim(), aMap.containsKey(ss.trim())?aMap.get(ss.trim())+1:1);
			}
		}
		List<String> userBQ=articleService.statisticsBQByUser();
		for(String s:userBQ){
			for(String ss:s.split(",")){
				uMap.put(ss.trim(), uMap.containsKey(ss.trim())?uMap.get(ss.trim())+1:1);
			}
		}
		//删除空格
		aMap.remove("");
		//循环存入集合
		String[] as = new String[aMap.size()];
		aMap.keySet().toArray(as);
		String[] us = new String[uMap.size()];
		uMap.keySet().toArray(us);
		for(int i=0;i<as.length;i++){
			b:for(int j=0;j<us.length;j++){
				if(as[i].equals(us[j])){
					bqListTemp.add(new Object[]{aMap.get(as[i]),uMap.get(us[j]),as[i]});
					break b;
				}
				if(j==us.length-1){
					bqListTemp.add(new Object[]{aMap.get(as[i]),0,as[i]});
				}
			}
		}
		JsonSuccessObject.SUCCESS.setMessage("查询成功!");
		JsonSuccessObject.SUCCESS.setData(bqListTemp);
		return JsonSuccessObject.SUCCESS;
	}
	
}
